package com.cubetest;

import min3d.core.Object3dContainer;
import min3d.core.RendererActivity;
import min3d.parser.IParser;
import min3d.parser.Parser;
import min3d.vos.Light;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;

public class CubeTestActivity extends RendererActivity {
	/** Called when the activity is first created. */
	/** Called when the activity is first created. */
	private Object3dContainer objModel, mazeModel, enemyTank, bulletModel,
			leftModel, rightModel, kapow, dpad;
	float previousY = 0;
	boolean isTouch = false;
	float angle;

	public void initScene() {

		scene.lights().add(new Light());
		scene.lights().add(new Light());

		Light myLight = new Light();
		myLight.position.setX(25);
		myLight.position.setY(0);
		myLight.position.setZ(30);
		scene.lights().add(myLight);

		IParser parser = Parser.createParser(Parser.Type.OBJ, getResources(),
				"com.cubetest:raw/newtank_obj", true);
		parser.parse();

		objModel = parser.getParsedObject();
		objModel.scale().x = objModel.scale().y = objModel.scale().z = .01f;
		objModel.rotation().y = 90f;
		objModel.position().y = 1f;
		objModel.position().z = 2.3f;

		IParser parser1 = Parser.createParser(Parser.Type.OBJ, getResources(),
				"com.cubetest:raw/newmaze1_obj", true);
		parser1.parse();

		mazeModel = parser1.getParsedObject();
		mazeModel.scale().x = mazeModel.scale().y = mazeModel.scale().z = .01f;
		mazeModel.position().x = 0.25f;

		IParser parserx = Parser.createParser(Parser.Type.OBJ, getResources(),
				"com.cubetest:raw/newmaze_obj", true);
		parserx.parse();

		bulletModel = parserx.getParsedObject();
		bulletModel.scale().x = bulletModel.scale().y = bulletModel.scale().z = 0.0015f;
		bulletModel.position().y = 1f;
		bulletModel.position().z = objModel.position().z;
		// bulletModel.position().z = 2.5f;

		IParser parser2 = Parser.createParser(Parser.Type.OBJ, getResources(),
				"com.cubetest:raw/newtank_obj", true);
		parser2.parse();

		enemyTank = parser2.getParsedObject();
		enemyTank.scale().x = enemyTank.scale().y = enemyTank.scale().z = 0.01f;
		enemyTank.position().x = 1f;
		enemyTank.position().y = 1f;
		enemyTank.position().z = -2f;
		enemyTank.rotation().y = -90f;

		IParser parser3 = Parser.createParser(Parser.Type.OBJ, getResources(),
				"com.cubetest:raw/dice_obj", true);
		parser3.parse();

		leftModel = parser3.getParsedObject();
		leftModel.scale().x = leftModel.scale().y = leftModel.scale().z = 0.23f;
		leftModel.rotation().x = 135;
		leftModel.rotation().z = -20f;

		leftModel.position().x = -2.5f;
		leftModel.position().z = 1f;
		leftModel.position().y = 0.5f;

		IParser parser4 = Parser.createParser(Parser.Type.OBJ, getResources(),
				"com.cubetest:raw/dice_obj", true);
		parser4.parse();

		rightModel = parser4.getParsedObject();
		rightModel.scale().x = rightModel.scale().y = rightModel.scale().z = 0.23f;
		rightModel.rotation().x = 135;
		rightModel.rotation().z = 17f;

		rightModel.position().x = 2.5f;
		rightModel.position().z = 1f;
		rightModel.position().y = 0.5f;

		IParser parser5 = Parser.createParser(Parser.Type.OBJ, getResources(),
				"com.cubetest:raw/kapow_obj", true);
		parser5.parse();

		kapow = parser5.getParsedObject();
		kapow.scale().x = kapow.scale().y = kapow.scale().z = 0.01f;
		// enemyTank.position().x = 1f;
		// enemyTank.position().y = 1f;
		// enemyTank.position().z = -1f;
		// enemyTank.rotation().y = -90f

		scene.addChild(objModel);
		// scene.addChild(mazeModel);
		scene.addChild(enemyTank);
		//scene.addChild(leftModel);
		//scene.addChild(rightModel);
		// scene.addChild(bulletModel);

		IParser parser6 = Parser.createParser(Parser.Type.OBJ, getResources(),
				"com.cubetest:raw/kapow_obj", true);
		parser6.parse();

		dpad = parser6.getParsedObject();
		dpad.scale().x = dpad.scale().y = dpad.scale().z = 0.03f;
		dpad.rotation().x = -90f;
		dpad.position().y = 1f;
		dpad.position().z = 2.3f;
		dpad.position().x = -1.2f;
		//scene.addChild(dpad);
		// scene.camera().position.x = 15f;
		scene.camera().position.y = 5f;
		//scene.camera().position.z = 15f;

	}

	public void updateScene() {
		// glSurfaceView.objModel.rotation().x++;
		// objModel.rotation().z++;
		// objModel.rotation().y++;
		// objModel.rotation().z++;// = 2.5f;
		// mazeModel.position().z+=0.05;
		//enemyTank.position().z += 0.009;
		//enemyTank.position().x -= 0.004;
		scene.removeChild(kapow);
		scene.removeChild(bulletModel);
		//scene.camera().position.z = objModel.position().z+2f; //attempt to move camera with model
		//scene.camera().position.x = objModel.position().x;
		// bulletModel.position().z = objModel.position().z;
		// bulletModel.position().x = objModel.position().x;

		if (bulletModel.position().z <= enemyTank.position().z
				&& bulletModel.position().x <= enemyTank.position().x) {

			scene.removeChild(enemyTank);
			kapow.position().x = enemyTank.position().x;
			kapow.position().y = enemyTank.position().y;
			kapow.position().z = enemyTank.position().z;

			scene.removeChild(bulletModel);
			scene.addChild(kapow);

		}

		// objModel.rotation().y+=1;
		// objModel.rotation().y-=2;
		// scene.camera().position.z += 0.01;
		// scene.camera().position.x++;//= 0.5f;
		// scene.camera().position.z+=0.03;//= 3.0f;
		// scene.camera().position.z-=0.3;//= 2.0f;
		scene.addChild(mazeModel);

		if (isTouch) {
			int step = 0;
			scene.addChild(bulletModel);
			while (step <= 13) {
				step++;
				bulletModel.position().x = objModel.position().x;
				bulletModel.position().z -= 0.12f;
				// bulletModel.position().z -= 0.17f;
			}
			// scene.removeChild(bulletModel);
			isTouch = false;

			// bulletModel.position().y = 1f;
			// bulletModel.position().z = 2.5f;

		}

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		float x = event.getX();
		float y = event.getY();
		float xx = (x - 100)/4000;
		float yy = (y - 380)/-4000;

		//float num = y - objModel.position().y;
		//float den = x - objModel.position().x;

		angle = (float) Math.atan2(yy, xx);
		angle *= (180 /3.14);

		// if(x >= 705 && x<= 785 && y>= 0 && y<=480){
		// objModel.position().x += 0.1f;
		// if(objModel.position().x >= 1.5f) objModel.position().x = 1.5f;
		// }

		// else if(x >= 5 && x<= 85 && y>= 0 && y<=480){
		// objModel.position().x -= 0.1f;
		// if(objModel.position().x <= -1.5f) objModel.position().x = -1.5f;
		// }

		 if(x >= 0 && x<= 200 && y>= 280 && y<=480){
		 objModel.position().x += xx;
		 objModel.position().z -= yy;
		 objModel.rotation().y = angle;
		 if(objModel.position().x <= -1.55f) 
			 {
			 objModel.position().x = -1.55f;
			 }
		 
		 else if(objModel.position().x >= 1.55f) objModel.position().x = 1.55f;
		 else if(objModel.position().z >= 2.3f) objModel.position().z = 2.3f;
		 }
		 
		 
		else {
			isTouch = true;
		}
		/*
		 * if(x <= 400){ previousY = Math.abs(180 - y); if(previousY <= 90){
		 * //previousY = Math.abs(180 - y); objModel.rotation().y = 90 +
		 * previousY; } else{ //previousY = Math.abs(180-y);
		 * objModel.rotation().y = previousY; }
		 * 
		 * 
		 * 
		 * } else{
		 * 
		 * previousY = Math.abs(180 - y); if(previousY <= 90){
		 * 
		 * objModel.rotation().y = Math.abs(90 - previousY); } else{ //previousY
		 * = Math.abs(180-y); objModel.rotation().y = -previousY + 90; }
		 * 
		 * 
		 * }
		 */

		// objModel.rotation().y = 90 + previousY;

		return true;

	}

	public void detectCollision() {

	}
}